package com.ericsson.smart.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.ericsson.smart.dto.GeoDrilldown;
import com.ericsson.smart.dto.GeographyDimension;

public class GeographyDimensionsDAO implements GeographyDimensionsInterface {

	//EntityManagerFactory emf = null;

	//EntityManager em = null;
	
	EntityManager entityManager = null;

	Object[][] results = null;

	public GeographyDimensionsDAO() {
		//emf = Persistence.createEntityManagerFactory("hello");
//		/em = emf.createEntityManager();
	}
	
	public EntityManager getEntityManager() {
        return entityManager;
    }
	
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
	

	@Override
	public void insert(GeographyDimension geod) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(GeographyDimension geod) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(int geoId) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object[][] select(String currDim, String condition) {

		GeoDrilldown g = GeoDrilldown.valueOf(currDim);
		String nextDimColName = g.getNextDimension().getColName();

		String queryStr = "select " + nextDimColName + "," + "count(" + nextDimColName
				+ ") from GeographyDimensions ";

		queryStr = queryStr + "where " + condition;
		queryStr = queryStr + " group by " + nextDimColName;

		System.out.println("queryStr :: " + queryStr);
		Query query = entityManager.createQuery(queryStr);

		@SuppressWarnings("unchecked")
		List<Object[]> results = query.getResultList();

		Object[][] resultArray = new Object[results.size()][];

		for (int i = 0; i < results.size(); i++) {
			resultArray[i] = results.get(i);
		}

		return resultArray;
	}

}
